<template>
  <div class="header">
    <div class="header-flex">
      <div class="header-lf">abcd69</div>
      <div class="header-rg">
        <div
          class="lang-box"
          @click="(showLangBox = !showLangBox), (showCountryBox = false)"
        >
          <div class="lang-title">{{ lang }}</div>
          <img
            class="bottom-icon-img"
            src="../assets/images/home/bottom-icon.png"
            alt=""
          />
        </div>
        <div
          class="area-box"
          @click="(showCountryBox = !showCountryBox), (showLangBox = false)"
        >
          <img
            class="area-icon-img"
            src="../assets/images/home/area-icon.png"
            alt=""
          />

          <div class="area-title">{{ country }}</div>
        </div>
      </div>
    </div>
    <div class="lang-popup-box" v-if="showLangBox">
      <div
        class="lang-item"
        v-for="item in langList"
        :key="item.id"
        @click="handleLang(item)"
      >
        {{ item }}
      </div>
    </div>
    <div class="country-popup-box" v-if="showCountryBox">
      <div
        class="country-item"
        v-for="item in countryList"
        :key="item"
        @click="handleCountry(item)"
      >
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "titleBar",
  data() {
    return {
      lang: "EN",
      country: "USA",
      showLangBox: false,
      showCountryBox: false,
      langList: ["English", "Español", "中文"],
      countryList: [
        "USA",
        "Canada",
        "UK",
        "Australia",
        "New Zealand",
        "Mexico",
      ],
    };
  },
  created() {},
  mounted() {
    let lang = localStorage.getItem("i18n_lang");
    if (lang) {
      if (lang == "zh-en") {
        this.lang = "EN";
      } else if (lang == "zh-es") {
        this.lang = "ES";
      } else if (lang == "zh-cn") {
        this.lang = "CN";
      }
    } else {
      this.lang = "EN";
    }
    console.log(this.lang);

    let country = localStorage.getItem("country");
    if (country) {
      this.country = country;
    } else {
      this.country = "USA";
    }
    console.log(this.country);
  },
  methods: {
    handleLang(item) {
      if (item == "English") {
        this.lang = "EN";
        localStorage.setItem("i18n_lang", "zh-en");
        this.$i18n.locale = "zh-en";
      } else if (item == "Español") {
        this.lang = "ES";
        localStorage.setItem("i18n_lang", "zh-es");
        this.$i18n.locale = "zh-es";
      } else if (item == "中文") {
        this.lang = "CN";
        localStorage.setItem("i18n_lang", "zh-cn");
        this.$i18n.locale = "zh-cn";
      }
      this.showLangBox = false;
    },
    handleCountry(item) {
      this.country = item;
      localStorage.setItem("country", this.country);
      this.showCountryBox = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  position: relative;
  .header-flex {
    height: 99px;
    background: #273458;
    padding: 15px 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    .header-lf {
      font-size: 50px;
      font-weight: 800;
      color: #ffffff;
    }
    .header-rg {
      display: flex;
      align-items: center;
      .lang-box {
        display: flex;
        align-items: center;
        margin-right: 46px;
      }
      .lang-title {
        font-size: 29px;
        font-weight: 400;
        color: #ffffff;
        margin-right: 11px;
      }
      .bottom-icon-img {
        width: 18.22px;
        height: auto;
      }
      .area-box {
        display: flex;
        align-items: center;
      }
      .area-icon-img {
        width: 48px;
        height: 48px;
        margin-right: 8px;
      }
      .area-title {
        font-size: 28px;
        font-weight: 400;
        color: #ffffff;
      }
    }
  }
  .lang-popup-box {
    position: absolute;
    width: 289px;
    height: 248px;
    background: #ffffff;
    z-index: 1;
    left: 40%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }
  .lang-item {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26px;
    font-weight: 400;
    color: #000000;
    box-sizing: border-box;
    border-bottom: 1px solid #d5d5d5;
  }
  .lang-item:last-child {
    border-bottom: none;
  }
  .country-popup-box {
    position: absolute;
    width: 289px;
    height: 325px;
    background: #ffffff;
    z-index: 1;
    box-sizing: border-box;
    right: 10px;
    display: flex;
    flex-direction: column;
  }
  .country-item {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    font-size: 26px;
    font-weight: 400;
    color: #000000;
    box-sizing: border-box;
    border-bottom: 1px solid #d5d5d5;
  }
  .country-item:last-child {
    border-bottom: none;
  }
}
</style>
